<?php

class Category extends AppModel {

    var $name = 'Category';
    var $belongsTo = array(
        'User' => array(
            'foreignKey' => 'collector_id',
            'fields' => array('name')
        ),
    );
    var $validate = array(
        'code' => array(
            'minLength' => array(
                'rule' => array('minLength', 3),
                'required' => true,
                'message' => 'Category code must have 3 characters.',
            ),
            'isUnique' => array(
                'rule' => 'isUnique',
                'message' => 'This category code is already taken.',
            ),
        ),
        'name' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
                'message' => 'Category name is required.',
                'required' => true,
            ),
            'isUnique' => array(
                'rule' => 'isUnique',
                'message' => 'This category name is already taken.',
            ),
        ),
    );

    /**
     * ถ้าเป็น vendor เข้ามาดู โชว์แค่ category ของตัวเอง
     * ถ้าเป็น ITS หรือ Customer โชว์เฉพาะที่มีใน CustProductSite
     * @param <type> $site_id
     * @param <type> $supplier_id
     */
    function listCategoryBySiteOrSupplier($site_id, $supplier_id = null) {
        if ($supplier_id != null) {
            // ดึงรายการ Customer ทั้งหมด ที่มี Product ของ Supplier ใน Site นี้
            $sql = "SELECT DISTINCT p.category_id
                     FROM pre_cust_products_sites cps JOIN pre_products p ON (cps.product_id = p.id)
                     WHERE cps.supplier_id = {$supplier_id}
                     AND site_id = {$site_id}
                     AND dispatch_from = 'supplier'";
        } else {
            $sql = "SELECT DISTINCT category_id
                    FROM pre_cust_products_sites cps
                    JOIN pre_products p ON (cps.product_id = p.id)
                    WHERE site_id = {$site_id} ";
        }

        $CPSs = $this->query($sql);
        $conditions = array();
        foreach ($CPSs as $k => $cps) {
            foreach ($cps as $k => $v) {
                $conditions[] = $v['category_id'];
            }
        }
        return $this->find('list', array('conditions' => array('id' => $conditions), 'order' => 'Category.name'));
    }

    /**
     * vendor เข้ามาดู โชว์แค่ category ของตัวเอง
     * @param <type> $site_id
     * @param <type> $supplier_id
     */
    function listCategoryBySupplierId($supplier_id) {
        $sql = "SELECT DISTINCT p.category_id
                     FROM pre_cust_products_sites cps JOIN pre_products p ON (cps.product_id = p.id)
                     WHERE cps.supplier_id = {$supplier_id}
                     AND dispatch_from = 'supplier'";
        $CPSs = $this->query($sql);
        $conditions = array();
        foreach ($CPSs as $k => $cps) {
            foreach ($cps as $k => $v) {
                $conditions[] = $v['category_id'];
            }
        }
        return $this->find('list', array('conditions' => array('id' => $conditions), 'order' => 'Category.name'));
    }

}